package info.tsukaworld.gui;

import acm.graphics.*;
import java.awt.Color;
import info.tsukaworld.actor.*;
import info.tsukaworld.grid.*;
import info.tsukaworld.world.*;
import info.tsukaworld.actor.mob.*;

public class PlayArea extends GCompound
{
    public static final int CELL_SIZE = 50;
    public static final int NUM_ROWS = 13;
    public static final int NUM_COLS = 13;
    public static final int BASE_HEIGHT = TopHUD.TOP_HUD_HEIGHT;
    public static final int MAX_WIDTH = NUM_ROWS * CELL_SIZE;
    public static final int MAX_HEIGHT = NUM_ROWS * CELL_SIZE + BASE_HEIGHT;
    public static ActorWorld world;
    private int currentDir = 0;
    private Player player;
    int xOffset = 0;
    int yOffset = 0;

    public PlayArea(Player a)
    {
        drawGrid();
        player = a;
        world = new BasicWorld(new BoundedGrid<Actor>(NUM_ROWS, NUM_COLS), player, this);
        drawTerrain();        
        world.add(world.getRandomEmptyLocation() , new ControlledMob());
        drawWorld();
    }

    public void drawGrid()
    {
        for(int r = 0; r < NUM_ROWS; r++)
        {
            for(int c = 0; c < NUM_COLS; c++)
            {
                //GRect rect = new GRect(50, 50);
                GImage g = new GImage("CaveFloor.gif");
                g.setSize( CELL_SIZE, CELL_SIZE );
                //rect.setColor( Color.RED );
                add( g, r * CELL_SIZE , c * CELL_SIZE + BASE_HEIGHT);
                //add( rect, r * CELL_SIZE , c * CELL_SIZE + BASE_HEIGHT);
            }
        }
    }
    
    public void drawTerrain()
    {
        for(int r = 0; r < world.getGrid().getNumRows(); r++)
        {
           for(int c = 0; c < world.getGrid().getNumCols(); c++)
            {
                if( world.getGrid().get(new Location(r, c)) != null  && world.getGrid().get(new Location(r, c)) instanceof Terrain)
                {
                    Actor a = (Actor)world.getGrid().get(new Location(r, c));
                    GImage g = a.getImage();
                    g.setSize(CELL_SIZE, CELL_SIZE);
                    add( g, c * CELL_SIZE,  r * CELL_SIZE + BASE_HEIGHT);
                }
            }
        }
    }

    public void drawWorld()
    {
        for(int r = 0; r < world.getGrid().getNumRows(); r++)
        {
           for(int c = 0; c < world.getGrid().getNumCols(); c++)
            {
                if( world.getGrid().get(new Location(r, c)) != null )
                {
                    Actor a = (Actor)world.getGrid().get(new Location(r, c));
                    GImage g = a.getImage();
                    g.setSize(CELL_SIZE, CELL_SIZE);
                    if( a instanceof MovingActor)
                    {
                        int dir = a.getDirection();
                        if( currentDir != dir)
                        {
                            currentDir = dir;
                        }
                        if(((AnimatedActor)a).isMoving() && ((AnimatedActor)a).getCurrentMove() % 60 == 0)
                        {
                            add( g, c * CELL_SIZE + ((MovingActor)a).getXoffset(), r * CELL_SIZE + BASE_HEIGHT + ((MovingActor)a).getYoffset());
                            ((MovingActor)a).setXoffset(0);
                            ((MovingActor)a).setYoffset(0);
                        }
                        else
                        {
                            switch( dir )
                            {
                                case 0: ((MovingActor)a).setYoffset( -(CELL_SIZE*((AnimatedActor)a).getCurrentMove()/((MovingActor)a).getMoveSpeed()));
                                break;
                                case 90: ((MovingActor)a).setXoffset( CELL_SIZE*((AnimatedActor)a).getCurrentMove()/((MovingActor)a).getMoveSpeed());
                                break;
                                case 180: ((MovingActor)a).setYoffset( CELL_SIZE*((AnimatedActor)a).getCurrentMove()/((MovingActor)a).getMoveSpeed());
                                break;
                                case 270: ((MovingActor)a).setXoffset( -(CELL_SIZE*((AnimatedActor)a).getCurrentMove()/((MovingActor)a).getMoveSpeed()));
                                break;
                            }
                            add( g, c * CELL_SIZE + ((MovingActor)a).getXoffset(), r * CELL_SIZE + BASE_HEIGHT + ((MovingActor)a).getYoffset());
                        }
                        
                    }
                }
            }
        } 
    }

    public void step()
    {
        for(int r = 0; r < world.getGrid().getNumRows(); r++)
        {
            for(int c = 0; c < world.getGrid().getNumCols(); c++)
            {
                Actor a = (Actor)world.getGrid().get(new Location(r, c));
                if( a != null && a instanceof MovingActor )
                {
                    remove(a.getImage());
                }
            }
        }
        world.step();
        drawWorld();
    }
    
}